package com.cn.applet.service.imp;

import com.cn.applet.dao.mapper.CoreUserMapper;
import com.cn.applet.dao.model.CoreUser;
import com.cn.applet.service.CoreUserService;
import com.cn.applet.utils.AppletMessage;
import com.cn.applet.utils.AppletStringUtils;
import com.cn.applet.utils.vo.ResultResponse;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("coreUserService")
public class CoreUserServiceImpl implements CoreUserService{
	
	private final static String regexEmail = "^[A-Za-z\\d]+([-_.][A-Za-z\\d]+)*@([A-Za-z\\d]+[-.])+[A-Za-z\\d]{2,4}$";
	
	//@Autowired
	//private CoreUserDAO coreUserDAO;
	
	@Autowired
	private CoreUserMapper userMapper;

	@Override
	@Transactional
	public ResultResponse updateUser(String userId, String username, String phone, String email) throws Exception {
		
		ResultResponse response = new ResultResponse();
		response.setMessage(AppletMessage.UPDATE_DATA_FAIL);
		
		if (StringUtils.isNotBlank(email) && !email.matches(regexEmail)) {
			response.setMessage(AppletMessage.EMAIL_FORMAT_NOT_CORRECT);
			return response;
		}
		
		CoreUser oldUser = userMapper.selectById(userId);
		if (null == oldUser) {
			response.setMessage(AppletMessage.OLD_DATA_NOT_EXIT);
			return response;
		}
		
		oldUser.setUsername(username);
		oldUser.setEmail(email);
		oldUser.setPhone(phone);
		oldUser.setUpdateDate(new Date());
		
		userMapper.updateUser(oldUser);
		
		response.setResult(true);
		response.setMessage(AppletMessage.UPDATE_DATA_SUCCESS);
		
		return response;
	}

	@Override
	@Transactional
	public ResultResponse selectById(String userId) throws Exception {
		
		ResultResponse response = new ResultResponse();
		response.setMessage(AppletMessage.GET_DATA_FAIL);
		
		if (StringUtils.isBlank(userId)) {
			response.setMessage(AppletMessage.MISSING_REQUIRED_PARAMETERS);
			return response;
		}
		CoreUser user = userMapper.selectById(userId);
		
		response.setResult(true);
		response.setMessage(AppletMessage.GET_DATA_SUCCESS);
		response.setResultData(user);
		
		return response;
	}

	@Override
	@Transactional
	public ResultResponse selectAll(Integer pageSize, Integer pageIndex) throws Exception {
		ResultResponse response = new ResultResponse();
		response.setMessage(AppletMessage.GET_DATA_FAIL);

		Map<String,Object> page = Maps.newHashMap();
		page.put("pageIndex",(pageIndex-1) * pageSize);
		page.put("pageSize",pageSize);

		List<CoreUser> userList = userMapper.selectAll(page);
		
		response.setResult(true);
		response.setMessage(AppletMessage.GET_DATA_SUCCESS);
		response.setResultData(userList);
		
		return response;
	}

	@Override
	@Transactional
	public ResultResponse addUser(String password, String phone, String username, String email) throws Exception {
		
		ResultResponse response = new ResultResponse();
		response.setMessage(AppletMessage.UPDATE_DATA_FAIL);
		
		if (StringUtils.isNotBlank(email) && !email.matches(regexEmail)) {
			response.setMessage(AppletMessage.EMAIL_FORMAT_NOT_CORRECT);
			return response;
		}
		
		CoreUser user = new CoreUser();
		user.setPassword(password);
		user.setPhone(phone);
		user.setUsername(username);
		user.setEmail(email);
		
		user.setId(AppletStringUtils.uuid());
		userMapper.insertUser(user);
		
		response.setResult(true);
		response.setMessage(AppletMessage.UPDATE_DATA_SUCCESS);
		
		return response;
	}

	@Override
	@Transactional
	public ResultResponse deleteUser(String userId) throws Exception {
		
		ResultResponse response = new ResultResponse();
		response.setMessage(AppletMessage.UPDATE_DATA_FAIL);
		
		if (StringUtils.isBlank(userId)) {
			response.setMessage(AppletMessage.MISSING_REQUIRED_PARAMETERS);
			return response;
		}
		
		int effortNum = userMapper.deleteUser(userId);
		if (0 < effortNum) {
			response.setResult(Boolean.TRUE);
			response.setMessage(AppletMessage.UPDATE_DATA_SUCCESS);
		}
		return response;
	}
}
